const {readJson,writeJson} = require('../../until/fs');
const {USER_JSON} = require('../../config/config.data');

// 列表
let getUserList = (req,res) =>{
    let data = readJson(USER_JSON)
    res.send({code:1,msg:"M站用户数据请求成功",data})
}

// 设置黑名单
let setUserStatus =(req,res) =>{
    const {user} = req.body;
    let userData = readJson(USER_JSON);
    let index = userData.findIndex(item=>item.user === user)
    if(index === -1){
        res.send({code:0,msg:"操作失败"})
        return
    }
    userData[index].status = userData[index].status === 'ok'?'no':'ok';
    writeJson(USER_JSON,userData)
    res.send({code:1,msg:"修改成功",data:userData})
}

// 搜索姓名
let searchName =(req,res)=>{
    const {name} = req.body
    let userData = readJson(USER_JSON);
        userData = userData.filter(item=>item.name.includes(name))
    res.send({code:1,msg:"请求成功成功",data:userData})
}

module.exports = {
    getUserList,setUserStatus,searchName
}